<extend name="Public:main" />
<block name="title">买入中心</block>
<block name="body">
<body ng-app="myApp" ng-controller="Ctrl" ng-cloak>
    <div class="nav_top flex">
        <a  href="/index.php/Home/Transaction/purchase">
            <i></i>
        </a>
        <div class="flex1">
            买入中心
        </div>
        <a class="more_show" href="javascript:void(0)">

        </a>

    </div>
    <div style="height: 0.44rem;"></div>
    <p class="l_tit">点击选择匹配金额</p>
    <input type="hidden" id="bankcardid" value="{$bankcardid}">
    <div class="cz_list clearfix">
        <ul>
            <volist name="transaction_num" id="vo1" key="k">
                <li ng-click="loadMore({$vo1})" value="<php>echo $vo1;</php>" <if condition="$k eq 1">class="active5"</if> >
                    {$vo1}
                </li>
            </volist>
        </ul>
    </div>
    <div style="height: 0.1rem;"></div>
    <div class="pay_list">
        <ul>
            <li class="flex" ng-repeat="log in logs">
                <div ng-if="log.profilephoto == '' || log.profilephoto == null">
                    <img src="__PUBLIC__/vpay/images/shezhi_icon_head.png">
                </div>
                <div ng-if="log.profilephoto != ''">
                    <img ng-src="{{ log.profilephoto }}" style="width: 100%;height: 100%;border-radius: 50%">
                </div>
                <div class="flex1">
                    <label>
                        {{ log.seller }}
                    </label>
                    <p>
                        时间：{{ log.createtime }}
                    </p>
                </div>
                <div style="width: 0.7rem;overflow: hidden">
                    <label>{{ log.entrustnum }}</label>
                    <button ng-click="buy(log.id)">
                        购买
                    </button>
                </div>
            </li>
        </ul>
    </div>


</body>
    <block name="custom_js">
        <script>
            var app = getApp('myApp', []);
            app.controller('Ctrl', function ($scope, $http) {
                //金额
                $scope.num = 500;
                // 当前页数
                $scope.p = 0;
                // 加载完毕标志
                $scope.endflg = 0;
                // 防止重复加载
                $scope.busy = false;
                // 存放数据
                $scope.logs = [];
                // 请求数据方法
                $scope.loadMore = function(num){

                    if(num != $scope.num) {
                        $scope.num = num;
                        // 当前页数
                        $scope.p = 0;
                        // 加载完毕标志
                        $scope.endflg = 0;
                        // 防止重复加载
                        $scope.busy = false;
                        // 存放数据
                        $scope.logs = [];
                    }

                    if($scope.endflg == 0){
                        if($scope.busy){
                            return false;
                        }
                        $scope.busy = true;
                        $http({
                            method:'POST',
                            data:{
                                "p" : $scope.p,
                                "num" : $scope.num,
                            },
                            headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
                            transformRequest: function(obj) {
                                var str = [];
                                for (var p in obj) {
                                    str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
                                }
                                return str.join("&");
                            },
                            url: "/index.php/Home/Transaction/buy_center?p="+$scope.p,
                        }).then(function successCallback(response) {
                            $scope.busy = false;
                            if(response.data.data != '' && response.data.data != null){
                                if($scope.p > 0) {
                                    layer.msg("正在加载...", {time: 500});
                                }
                            } else {
                                layer.msg("已经全部加载完了...", {time:500});
                                $scope.endflg = 1;
                                return false;
                            }
                            $scope.p++;
                            if(response.data.code == -1){
                                layer.msg(response.data.msg,{time:1500})
                            }else{
                                if(response.data.data == null){
                                    layer.msg(response.data.msg,{time:1500});
                                } else {
                                    // 组织数据
                                    for (var i in response.data.data) {
                                        $scope.logs.push(response.data.data[i]);
                                    }
                                }
                            }
                        }, function errorCallback(response) {
                            errorHandle(response);
                        });
                    }else{
                        layer.msg("已经全部加载完了...", {time:500});
                    }
                };
                // 进页面先加载一次数据
                $scope.loadMore($scope.num);

                $(window).scroll(function(){
                    var scrollTop = $(this).scrollTop();
                    var scrollHeight = $(document).height();
                    var windowHeight = $(this).height();
                    if(scrollTop + windowHeight == scrollHeight){
                        $scope.loadMore($scope.num);
                    }
                });

                $scope.buy=function (id) {
                    layer.confirm('需要打款给用户，确认购买？', {
                        btn: ['确认','取消'] //按钮
                    }, function(){
                        $http({
                            method:'POST',
                            data:{
                                "id" : id,
                                "bankcardid" : $("#bankcardid").val(),
                            },
                            headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
                            transformRequest: function(obj) {
                                var str = [];
                                for (var p in obj) {
                                    str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
                                }
                                return str.join("&");
                            },
                            url: "/index.php/Home/Transaction/center_buyin"
                        }).then(function successCallback(response) {
                            if(response.data.code==0){
                                layer.msg("操作成功！");
                                setTimeout(function () {
                                    window.location.href = '/index.php/Home/Transaction/confirmOrderList';
                                }, 1000);
                            }else {
                                if(response.data.msg != '' && response.data.msg != null) {
                                    layer.msg(response.data.msg,{time:1500})
                                }
                            }
                        }, function errorCallback(response) {
                            errorHandle(response);
                        });
                    }, function() {
                        layer.msg('已取消');
                    })
                }

            });
        </script>
    </block>
</block>